package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink06_Window_ProcessFunction {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env
                .socketTextStream("hadoop162", 9999)
                .map(line ->{
                    String[] strings = line.split(",");
                    return new WaterSensor(
                            strings[0],
                            Long.valueOf(strings[1]),
                            Integer.valueOf(strings[2]));
                })

                .keyBy(WaterSensor::getId)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<WaterSensor>() {
                            @Override
                            public WaterSensor reduce(WaterSensor ws1,
                                                      WaterSensor ws2) throws Exception {
                                System.out.println("xxxxx");
                                ws1.setVc(ws1.getVc() + ws2.getVc());
                                return ws1;
                            }
                        }
                        , new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                            @Override
                            public void apply(String value,
                                              TimeWindow window,
                                              Iterable<WaterSensor> input, //reduce是增量聚合，不保存之前的值，这里面的存的值是前面的最终结果，有且仅有一个值
                                              Collector<String> out) throws Exception {
                                WaterSensor result = input.iterator().next();
                                System.out.println("yyyyyy");
                                out.collect(window+" "+result);
                            }
                        })
                .print();


        env.execute();
    }
}
